package com.xxx.video.dynamic.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.configs.Constant;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.SPUtil;
import com.xxx.video.commen.views.AddFriendExtraMessageDialog;
import com.xxx.video.dynamic.beans.FriendListBean;
import com.xxx.video.mine.beans.DynamicListBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class DynamicPresenter extends BasePresenter<IDynamicView> {

    public DynamicPresenter(@NonNull Context context, IDynamicView baseView) {
        super(context, baseView);
    }

    /**
     * get dynamic list
     *
     * @param page page
     */
    public void getDynamicList(int page) {
        if (!UserManager.getInstance().isLogin()) {
            return;
        }
        NetWorkUtils.getInstance().getDynamicList("", String.valueOf(page), new CommonResponseListener<DynamicListBean>() {
            @Override
            public void onSuccess(DynamicListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        if (!ListUtils.isEmpty(successOb.page.list) && page == 1) {
                            SPUtil.putString(Constant.DynamicList, new Gson().toJson(successOb));
                        }
                        getBaseView().initData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    String result = SPUtil.getString(Constant.DynamicList, "");
                    if (!TextUtils.isEmpty(result) && page == 1) {
                        try {
                            DynamicListBean successOb = new Gson().fromJson(result, DynamicListBean.class);
                            getBaseView().initData2View(successOb);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    /**
     * get recommend  friend list
     */
    public void recommendFriends() {
        NetWorkUtils.getInstance().recommendFriends(String.valueOf(1), new CommonResponseListener<FriendListBean>() {
            @Override
            public void onSuccess(FriendListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initRecommendFriendList(successOb.page);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
            }
        });
    }

    /**
     * get near by user
     *
     * @param page page
     * @param lon  lon
     * @param lan  lan
     */
    public void nearbyUser(String page, String lon, String lan) {
        NetWorkUtils.getInstance().nearbyUser(page, lon, lan, new CommonResponseListener<FriendListBean>() {
            @Override
            public void onSuccess(FriendListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initPeopleNearbyList(successOb.page);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
            }
        });
    }

    /**
     * add friend
     *
     * @param listBean data
     * @param position position
     * @param type     type
     */
    public void addFriends(FriendListBean.PageBean.ListBean listBean, int position, int type) {
        AddFriendExtraMessageDialog addFriendExtraMessageDialog = new AddFriendExtraMessageDialog(context);
        addFriendExtraMessageDialog.show();
        addFriendExtraMessageDialog.setOnButtonClickListener(message -> NetWorkUtils.getInstance().addFriends(listBean.userId, listBean.username, listBean.nickName, listBean.picUrl, message, new CommonResponseListener<BaseBean>() {

            @Override
            public void onSuccess(BaseBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().addFriendSuccess(position, type);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        }));

    }
}
